﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SedionFormDesigner.CusControls.Infrastructure;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using SedionFormDesigner.CusControls.TypeEditor;
using System.Reflection;
using SedionFormDesigner.CusControls.DataGridView;

namespace SedionFormDesigner.CusControls
{
    public class BD_DataGridView : System.Windows.Forms.DataGridView
    {
        public BD_DataGridView()
            : base()
        {
            base.AllowUserToAddRows = true;
        }

        #region 属性:外观

        private float _fontsize = 9;//字体大小
        private bool _fontbold = false;//是否粗体
        private bool _fontitalic = false;//是否斜体
        private TextAlign _textAlign;
        private OprType _operType;
        private String _sql;
        private DataGridViewColumnCollection _columns = null;       // 绑定数据列
        private Object _data = null;                                // 表单填写的数据
        private DataGridViewRowCollection _rows = null;             // 绑定数据行

        public static Int32 Count = 0;                              // 当前设计时该控件的个数
        private String _name = String.Empty;

        [Category("设计")]
        [ReadOnly(true)]
        public String NewName
        {
            get
            {
                _name = _name == String.Empty ? base.Name : _name;
                return _name;
            }
            set
            {
                _name = value;
                base.Name = _name;
            }
        }

        [Category("外观")]
        public float FontSize
        {
            set { _fontsize = value; FontChange(); }
            get { return _fontsize; }
        }

        [Category("外观")]
        public bool FontBold
        {
            set { _fontbold = value; FontChange(); }
            get { return _fontbold; }
        }

        [Category("外观")]
        public bool FontItalic
        {
            set { _fontitalic = value; FontChange(); }
            get { return _fontitalic; }
        }

        #endregion

        void FontChange()
        {
            if (_fontbold && _fontitalic)
                base.Font = new Font(base.Font.FontFamily.Name, _fontsize, FontStyle.Bold | FontStyle.Italic);
            else if (_fontbold && !_fontitalic)
                base.Font = new Font(base.Font.FontFamily.Name, _fontsize, FontStyle.Bold);
            else if (!_fontbold && _fontitalic)
                base.Font = new Font(base.Font.FontFamily.Name, _fontsize, FontStyle.Italic);
            else
                base.Font = new Font(base.Font.FontFamily.Name, _fontsize);
        }

        [Category("杂项")]
        [DescriptionAttribute("添加列\r\n唯有选择操作类型为'手动填写'方有效")]
        [BrowsableAttribute(true)]
        [System.Runtime.CompilerServices.SpecialName]
        [RefreshProperties(RefreshProperties.All)]
        [Editor(typeof(ColumnEditor), typeof(UITypeEditor)), Localizable(false)]
        public new DataGridViewColumnCollection Columns
        {
            get { return _columns; }
            set
            {
                _columns = value;
            }
        }

        [Category("杂项")]
        [Description("数据源\r\n唯有选择操作类型为'绑定数据库'方有效")]
        [Browsable(true)]
        [Editor(typeof(DataSourceEditor), typeof(UITypeEditor)), Localizable(true)]
        public String SQL
        {
            get { return _sql; }
            set { _sql = value; }
        }

        [Category("数据")]
        [Description("填写数据")]
        [Browsable(true)]
        [Editor(typeof(DataBindingEditor), typeof(UITypeEditor)), Localizable(true)]
        public Object Data
        {
            get { return _data; }
            set
            {
                _data = value;
                var grdView = (System.Windows.Forms.DataGridView)_data;
                //CopyGrdViewData.AddCols(ref this._columns, grdView);

                var rowCount = grdView.Rows.Count;
                var colCount = grdView.Columns.Count;
                this.Rows.Clear();
                this.Rows.Add(rowCount - 1);
                for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
                {
                    for (int colIndex = 0; colIndex < colCount; colIndex++)
                    {
                        this.Rows[rowIndex].Cells[colIndex].Value = grdView.Rows[rowIndex].Cells[colIndex].Value;
                    }
                }
            }
        }

        [Category("杂项")]
        [Browsable(true)]
        [Description("操作类型")]
        public OprType OperType
        {
            get
            {
                return _operType;
            }
            set
            {
                if (value == OprType.手动填写)
                {
                    ControlHelper.SetPropertyVisibility(this, "Columns", true);
                    ControlHelper.SetPropertyVisibility(this, "SQL", false);
                    ControlHelper.SetPropertyVisibility(this, "Data", true);
                }
                else if (value == OprType.绑定数据库)
                {
                    ControlHelper.SetPropertyVisibility(this, "Columns", false);
                    ControlHelper.SetPropertyVisibility(this, "SQL", true);
                    ControlHelper.SetPropertyVisibility(this, "Data", false);
                }
                else if (value == OprType.可增表格)
                {
                    ControlHelper.SetPropertyVisibility(this, "Columns", true);
                    ControlHelper.SetPropertyVisibility(this, "SQL", false);
                    ControlHelper.SetPropertyVisibility(this, "Data", true);
                }
                this.Refresh();
                _operType = value;
            }
        }
    }
}
